#rootfs_dir=./ubuntu-jammy-bootstrap

rootfs_image=/home/coresat/runnig_kernel/runninglinuxkernel_5.0/rootfs_debian_arm64.ext4
rootfs_path=/home/coresat/qemu_debootstrap/tmp


rootfs_dir=$PWD/ubuntu-jammy-bootstrap
kernel_build=$rootfs_dir/usr/src/linux
#kernel_dir=/home/coresat/qemu_aarch64_kernel/linux-6.8.1
kernel_dir=/home/coresat/qemu_aarch64_kernel/build_output

build_kernel_devel(){

	cd $kernel_dir
	kernver="$(cat include/config/kernel.release)"
	echo "kernel version: $kernver"

	mkdir -p $kernel_build
	rm $rootfs_dir/lib/modules/$kernver/build


	#need to copy hidden files (.config)
	cp -a . $kernel_build
	#cp source code
	# /home/coresat/qemu_aarch64_kernel/linux-6.8.1
	rm -rf $kernel_build/source
	mkdir -p $kernel_build/source
	echo "cp source to " $kernel_build/source
	cp -a /home/coresat/qemu_aarch64_kernel/linux-6.8.1/* $kernel_build/source

	#cp -a include $kernel_build
	#cp -a scripts $kernel_build

	#cp Makefile .config Module.symvers System.map vmlinux $kernel_build
	#mkdir -p $kernel_build/arch/arm64/
	#mkdir -p $kernel_build/arch/arm64/kernel/

	#cp -a arch/arm64/include $kernel_build/arch/arm64/
	#cp -a arch/arm64/Makefile $kernel_build/arch/arm64/

	# error for linux-6.8
	#cp: cannot stat 'arch/arm64/kernel/module.lds': No such file or directory
	#cp arch/arm64/kernel/module.lds $kernel_build/arch/arm64/kernel/

	ln -s /usr/src/linux $rootfs_dir/lib/modules/$kernver/build
	
	# ln to debian linux-5.0/scripts
	#ln -s /usr/src/linux-kbuild/scripts $rootfs_dir/usr/src/linux/scripts
	# ln to debian linux-6.8
	#ln -s /usr/src/linux/scripts $rootfs_dir/usr/src/linux/scripts
    cd -
}


# mount -t ext4 $rootfs_image $rootfs_path -o loop

# mkdir -p $rootfs_dir/lib/modules
# cp -af $rootfs_path/lib/modules/* $rootfs_dir/lib/modules

# mkdir -p $rootfs_dir/usr/src/linux
# cp -af $rootfs_path/usr/src/linux/* $rootfs_dir/usr/src/linux


# mkdir -p $rootfs_dir/usr/src/linux-kbuild/scripts
# cp -af $rootfs_path/usr/src/linux-kbuild/scripts/* $rootfs_dir/usr/src/linux-kbuild/scripts

mkdir -p $rootfs_dir/lib/modules
cp -af /home/coresat/qemu_aarch64_kernel/rootfs_debian_arm64/lib/modules/* $rootfs_dir/lib/modules

build_kernel_devel

cp -af /home/coresat/runnig_kernel/runninglinuxkernel_5.0/kmodules/rlk_lab/rlk_basic/chapter_1_quick_start/lab3_hello_world $rootfs_dir/home

#umount $rootfs_path


# no use, could be deleted
#cp /usr/bin/qemu-aarch64-static  $rootfs_dir/usr/bin

cp 03_config_rootfs.sh  $rootfs_dir/
mount -t proc /proc  $rootfs_dir/proc
mount -t sysfs /sys  $rootfs_dir/sys
mount -o bind /dev  $rootfs_dir/dev
mount -o bind /dev/pts  $rootfs_dir/dev/pts
LANG=zh_CN.UTF-8 chroot $rootfs_dir/ /bin/bash



